package devyouk.util.excel.cellAdapter;

import org.apache.poi.ss.usermodel.CellType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class DefaultCellValueAdapter {

    private static final StringToDoubleAdapter stringToDoubleAdapter = new StringToDoubleAdapter();
    private static final ObjcetToStringAdapter objcetToStringAdapter = new ObjcetToStringAdapter();
    private static CellValueAdapter<String, Date> cellValueAdapter;
    private static CellValueAdapter<String, Calendar> stringToCalendarAdapter;
    private static HashMap<Class, CellValueAdapter> adapterCache = new HashMap<>();
    private static CellValueAdapter<Number, Double> numberToDoubleAdapter;

    private DefaultCellValueAdapter() {
    }

    public static CellValueAdapter<String, Double> stringToDouble() {
        return stringToDoubleAdapter;
    }

    public static CellValueAdapter<Object, String> objectToString() {
        return objcetToStringAdapter;
    }

    public static CellValueAdapter<String, Date> stringToDate() {
        if (cellValueAdapter != null) {
            return cellValueAdapter;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        cellValueAdapter = new CellValueAdapter<String, Date>() {
            @Override
            public CellType getCellType() {
                return CellType.NUMERIC;
            }

            @Override
            public Date getCellValue(String data) {
                try {
                    return simpleDateFormat.parse(data);
                } catch (ParseException e) {
                    return null;
                }
            }
        };
        return cellValueAdapter;
    }

    public static CellValueAdapter<String, Calendar> stringToCalendar() {
        if (stringToCalendarAdapter != null) {
            return stringToCalendarAdapter;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        stringToCalendarAdapter = new CellValueAdapter<String, Calendar>() {
            @Override
            public CellType getCellType() {
                return CellType.NUMERIC;
            }

            @Override
            public Calendar getCellValue(String data) {
                try {
                    Calendar instance = Calendar.getInstance();
                    instance.setTime(simpleDateFormat.parse(data));
                    return instance;
                } catch (ParseException e) {
                    return null;
                }
            }
        };
        return stringToCalendarAdapter;
    }

    public static CellValueAdapter<Number, Double> numberToDouble() {
        if (numberToDoubleAdapter != null) {
            return numberToDoubleAdapter;
        }
        numberToDoubleAdapter = new CellValueAdapter<Number, Double>() {
            @Override
            public CellType getCellType() {
                return CellType.NUMERIC;
            }

            @Override
            public Double getCellValue(Number data) {
                return (Double) data;
            }
        };
        return numberToDoubleAdapter;
    }

    private static <T> CellValueAdapter<T, T> genericAdapter(Class<T> type, CellType cellType) {
        if (adapterCache.containsKey(type)) {
            return adapterCache.get(type);
        }
        CellValueAdapter<T, T> adapter = new CellValueAdapter<T, T>() {
            @Override
            public CellType getCellType() {
                return cellType;
            }

            @Override
            public T getCellValue(T data) {
                return data;
            }
        };
        adapterCache.put(type, adapter);
        return adapter;
    }

    public static <T> CellValueAdapter getAdapterOfType(Class<T> type) {
        if (type == Date.class || type == Calendar.class) {
            return genericAdapter(type, CellType.NUMERIC);
        } else if (Number.class.isAssignableFrom(type)) {
            return numberToDouble();
        } else if (type == String.class) {
            return genericAdapter(type, CellType.STRING);
        } else if (type == Boolean.class) {
            return genericAdapter(type, CellType.BOOLEAN);
        } else {
            return objectToString();
        }
    }
}
